#!/bin/python

#
#Xithyl, Test Driven Development for C
#Copyright (C) 2012  Stepan Henek
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import os

def runSafe(function, cond, *arg):
    """
    Run the function in a new process.

    @param function the function to be executed
    @param cond expected return value of the function
    @param *arg the arguments of the function

    @return the return value of the child process
    """
    newpid = os.fork()
    if newpid == 0:
        # Child branch
        ret = function(*arg)
        if cond == ret:
            os._exit(os.EX_OK)
        else:
            os._exit(os.EX_SOFTWARE)
    else:
        # Parent branch
        return os.waitpid(newpid, 0)[1]

